using System;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using System.Collections;
using mojoPortal.Business;
using Resources;

namespace ud_SwirlingQuestion
{

    public partial class ViewListPage : mojoBasePage
    {
        private int moduleId = -1;
        private int pageId = -1;
        SwirlingQuestionConfiguration config = new SwirlingQuestionConfiguration();

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            Load += Page_Load;
            LoadPanels();
            base.OnInit(e);
        }

        private void LoadPanels()
        {
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new SwirlingQuestionConfiguration(settings);
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting,
                            config.ShowBottomPanelSetting, config.ShowNumberModuleSetting);
        }
        
        #endregion

        private void Page_Load(object sender, EventArgs e)
        {
            LoadSettings();
            PopulateLabels();
            PopulateControls();
        }

        private void PopulateLabels()
        {
            PageTitle = SiteUtils.FormatPageTitle(siteSettings, SwirlingQuestionResource.SwirlingQuestionFeatureName);
            Module m = new Module(moduleId);
            moduleTitle.Header = m.ModuleTitle;
        }

        private void LoadSettings()
        {
            config.TitleOnly = false;
        }

        private void PopulateControls()
        {
            QuestionList.ModuleId = moduleId;
            QuestionList.PageId = pageId;
            QuestionList.Config = config;
            QuestionList.SiteRoot = SiteRoot;
            QuestionList.ImageSiteRoot = ImageSiteRoot;
            QuestionList.IsEditable = SiteUtils.UserCanEditModule(moduleId);
        }

        protected override void OnError(EventArgs e)
        {
            Exception lastError = Server.GetLastError();
            if ((lastError != null) && (lastError is NullReferenceException) && Page.IsPostBack)
            {
                if (lastError.StackTrace.Contains("Recaptcha"))
                {
                    Server.ClearError();
                    WebUtils.SetupRedirect(this, Request.RawUrl);

                }

            }


        }



    }
}
